/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.TrainDycxsjy;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.service.TrainDycxsjyService;
import me.zhengjie.domain.vo.TrainDycxsjyQueryCriteria;
import me.zhengjie.mapper.TrainDycxsjyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author huangwencong
* @date 2024-10-06
**/
@Service
@RequiredArgsConstructor
public class TrainDycxsjyServiceImpl extends ServiceImpl<TrainDycxsjyMapper, TrainDycxsjy> implements TrainDycxsjyService {

    private final TrainDycxsjyMapper trainDycxsjyMapper;

    @Override
    public TrainDycxsjy findOne(Long trainId, Long trainItemId) {
        return trainDycxsjyMapper.findOne(trainId, trainItemId);
    }

    @Override
    public PageResult<TrainDycxsjy> queryAll(TrainDycxsjyQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(trainDycxsjyMapper.findAll(criteria, page));
    }

    @Override
    public List<TrainDycxsjy> queryAll(TrainDycxsjyQueryCriteria criteria){
        return trainDycxsjyMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TrainDycxsjy resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TrainDycxsjy resources) {
        TrainDycxsjy trainDycxsjy = getById(resources.getId());
        trainDycxsjy.copy(resources);
        saveOrUpdate(trainDycxsjy);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<TrainDycxsjy> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TrainDycxsjy trainDycxsjy : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("训练ID", trainDycxsjy.getTrainId());
            map.put("结果判定(0:优秀,1:良好,2:合格,3:不合格)", trainDycxsjy.getResult());
            map.put("建议", trainDycxsjy.getJy());
            map.put("记录日期", trainDycxsjy.getDate());
            map.put("记录人", trainDycxsjy.getCreateBy());
            map.put("创建时间", trainDycxsjy.getCreateTime());
            map.put("更新时间", trainDycxsjy.getUpdateTime());
            map.put("训练项目ID", trainDycxsjy.getTrainItemId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}